package com.thon.controller.admin;

import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.thon.entity.project.ProjectCategory;
import com.thon.service.project.ProjectCategoryService;
import com.thon.service.project.ProjectService;

/** 
 * 类说明 
 * @author jiang
 * @date 创建时间：2014年7月11日 下午8:48:01 
 */
@Controller
@RequestMapping(value = "/api/system")
@SuppressWarnings({ "unchecked", "rawtypes" })
public class SystemSettingController {
	
	@Autowired
	private ProjectCategoryService projectCategoryService;
	@Autowired
	private ProjectService projectService;
	
	//(项目分类、项目收益、开放平台)的列表
	@RequestMapping(value="/project/categorylist",method = RequestMethod.GET)
	public ResponseEntity<?> listCategoryByType(@RequestParam(value="type") String type){
		
		List<ProjectCategory> list = projectCategoryService.listCategoriesByType(type);
		
		return new ResponseEntity(list,HttpStatus.OK);
	}
	//(项目分类、项目收益、开放平台)的添加
	@RequestMapping(value="/project/category/add",method = RequestMethod.POST)
	public ResponseEntity<?> addProjectCategory(@RequestParam(value="type") String type,
												@Valid ProjectCategory projectCategory){
		
		ProjectCategory category = new ProjectCategory();
		long amount = 0;
		category.setCategoryName(projectCategory.getCategoryName());
		category.setCategoryCode(projectCategory.getCategoryCode());
		category.setProjectAmount(amount);
		category.setType(type);
		category.setSort(projectCategory.getSort());
		category.setCategoryDesc(projectCategory.getCategoryDesc());
		projectCategoryService.addProjectCategory(category);
		return new ResponseEntity(true, HttpStatus.OK);
	}
	//(项目分类、项目收益、开放平台)的修改
	@RequestMapping(value="/project/category/update",method = RequestMethod.POST)
	public ResponseEntity<?> addProjectCategory(@Valid ProjectCategory projectCategory,
												@RequestParam(value="id") int id){
		
		ProjectCategory category = projectCategoryService.getProjectCategory(projectCategory.getId());
		int categoryId = category.getId();
		long amount = projectService.countProjectByCategory(categoryId);
		category.setCategoryName(projectCategory.getCategoryName());
		category.setCategoryCode(projectCategory.getCategoryCode());
		category.setProjectAmount(amount);
		category.setType(projectCategory.getType());
		category.setSort(projectCategory.getSort());
		category.setCategoryDesc(projectCategory.getCategoryDesc());
		projectCategoryService.updateProjectCategory(category);
		return new ResponseEntity(true, HttpStatus.OK);
	}
	//(项目分类、项目收益、开放平台)的删除
	@RequestMapping(value="/project/category/delete",method = RequestMethod.POST)
	public ResponseEntity<?> deleteCategory(@RequestParam(value="categoryId") int categoryId){
		projectCategoryService.deleteCategory(categoryId);
		return new ResponseEntity(true, HttpStatus.OK);
	}
}
